package com.echo.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;

import com.echo.activity.R;
import com.echo.activity.SportActivity;
import com.echo.activity.SportsActivity;
import com.echo.adapter.WeekDataAdapter;
import com.echo.model.DayData;
import com.echo.model.User;
import com.echo.util.DateUtil;
import com.echo.util.ScreenUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by EchoGe on 2016/4/23.
 */
public class SportsFragment extends Fragment{

    private Activity mParentActivity;
    private View mView;
    private Button btn;

    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private WeekDataAdapter mAdapter;

    private int mCurrentIndex=0;

    private PtrFrameLayout mPtrFrameLayout;

    private List<String> tabTitle=new ArrayList<String>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mParentActivity = (SportsActivity) getActivity();
        if (mView == null) {
            mView = inflater.inflate(R.layout.fragment_sports, container, false);
            initView(mView);
        }

        initViewPager();

        return mView;
    }

    private void initViewPager() {
        mTabLayout= (TabLayout) mView.findViewById(R.id.week_date);
        Calendar[] tabTitleCalendar= DateUtil.getThisWeekCalendar();
        for(int i=0;i<7;i++){
            tabTitle.add(String.valueOf(tabTitleCalendar[i].getTime().getDate()));
        }
        mViewPager= (ViewPager) mView.findViewById(R.id.datas_view_pager);
        mAdapter=new WeekDataAdapter(getChildFragmentManager(),tabTitleCalendar,tabTitle);
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        int today=Calendar.getInstance().getTime().getDay();
        if(today==0)
            today=7;
        mViewPager.setCurrentItem(today-1);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mPtrFrameLayout= (PtrFrameLayout) mView.findViewById(R.id.view_pager_ptr_frame);
        mPtrFrameLayout.disableWhenHorizontalMove(true);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                int position=mViewPager.getCurrentItem();
                mAdapter.refreshFragment(position);
                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPtrFrameLayout.refreshComplete();
                    }
                },1800);
            }
        });
    }

    private void initView(View mView) {
        btn= (Button) mView.findViewById(R.id.start_sport);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it=new Intent();
                it.setClass(getActivity(), SportActivity.class);
                startActivity(it);
            }
        });
    }
}
